/**
 * @Description MyPointController.java
 * @author xu_cc [18724000100@163.com]
 * @date 创建时间：2018年5月16日 下午5:51:57
 */
package com.solution.wx.my.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.solution.admin.point.constants.PointConstants;
import com.solution.admin.point.entity.PointEntity;
import com.solution.admin.point.service.PointLogService;
import com.solution.admin.point.service.PointService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.DateUtils;
import com.solution.common.utils.string.StringUtil;

/**
 * 我的积分
 * 
 * @author xu_cc
 * @date 创建时间：2018年5月16日 下午5:55:36
 */
@RequestMapping("/wx/point")
@Controller("myWxPointController")
public class MyPointController extends BaseController {

	@Autowired
	PointService pointService;

	@Autowired
	PointLogService pointLogService;

	/**
	 * 微信端-我的积分
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return String
	 * @author xu_cc
	 * @Date 创建时间：2018年5月16日 下午5:55:18
	 */
	@RequestMapping("/myPoint")
	public String toWxPointPage(HttpServletRequest request, HttpServletResponse response, Model model) {
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			String requestType = request.getParameter("requestType");
			
			String pageNumStr = request.getParameter("pageNum");
			if(StringUtil.isEmpty(pageNumStr)){
				pageNumStr = "1";
			}
			int pageNum = Integer.parseInt(pageNumStr);
			int numPerPage = 10;
			PageParam pageParam = super.getPageParam(request);
			pageParam.setPageNum(pageNum);
			pageParam.setNumPerPage(numPerPage);
			
			String operateFlag = request.getParameter("operateFlag");
			String month = request.getParameter("month");
			Long customerId = getCustomerId();

			paramMap.put("customerId", customerId);
			paramMap.put("operateFlag", StringUtil.isEmpty(operateFlag) ? null : operateFlag);
			paramMap.put("month", month);
			paramMap.put("terminalType", PointConstants.FRONTEND_VIEW_POINT_LOG);
			PageBean pageBean = pointLogService.listPointLogPage(pageParam, paramMap);
			if ("sync".equals(requestType)) {
				// 如果是异步，直接写response
				try {
					response.getWriter().write(JSONObject.toJSON(pageBean).toString());
					response.getWriter().flush();
					response.getWriter().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} else {
				// 初次加载，进入页面
				// 查询客户积分余额
				PointEntity point = pointService.getPointByCustomerId(customerId);
				// 已花费的所有积分合计
				Map<String, Object> totalPointParam = new HashMap<String, Object>();
				totalPointParam.put("customerId", customerId);
				totalPointParam.put("operateFlag", PointConstants.PointOperateFlag.FLAG_MINUS.getCode());
				// 客户总花费积分数
				BigDecimal costTotalPoint = pointLogService.getCustomerTotalPoint(totalPointParam);
				// 客户当月花费积分数
				if(StringUtil.isEmpty(month)) {
					totalPointParam.put("month", DateUtils.formatDate(new Date(), "yyyyMM")); 
				} else {
					totalPointParam.put("month", month); 
				}
				BigDecimal costTotalPointCurMonth = pointLogService.getCustomerTotalPoint(totalPointParam);
				// 客户当月赚取积分数
				totalPointParam.put("operateFlag", PointConstants.PointOperateFlag.FLAG_ADD.getCode());
				BigDecimal addTotalPointCurMonth = pointLogService.getCustomerTotalPoint(totalPointParam);

				model.addAttribute("month", totalPointParam.get("month"));
				model.addAttribute("costTotalPoint", costTotalPoint);
				model.addAttribute("costTotalPointCurMonth", costTotalPointCurMonth);
				model.addAttribute("addTotalPointCurMonth", addTotalPointCurMonth);
				model.addAttribute("point", point.getPoint());
				model.addAttribute("pointList", pageBean.getRecordList());
				model.addAttribute("pageBean", pageBean);
				model.addAttribute("requestType", requestType);
				model.addAttribute("operateFlag", operateFlag);
				// 加载月份下拉列表
				List<Map<String, String>> monthList = getMonthList();
				model.addAttribute("monthList", monthList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "wx/my/point/my_point";
	}

	/**
	 * 月份；列表
	 * 
	 * @return List<String>
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2018年5月16日 下午9:10:10
	 */
	private List<Map<String, String>> getMonthList() {
		List<Map<String, String>> monthList = new ArrayList<Map<String, String>>();
		try {
			Calendar c = Calendar.getInstance();
			int year = c.get(Calendar.YEAR);
			String yearStr = null;
			String monthStr = null;
			Map<String, String> monthMap = null;
			for (int i = 0; i < 2; i++) {
				yearStr = (year - i) + "";
				for (int j = 12; j > 0; j--) {
					if (j < 10) {
						monthStr = "0" + j;
					} else {
						monthStr = "" + j;
					}
					monthMap = new HashMap<String, String>();
					monthMap.put("month", yearStr.concat(monthStr));
					monthMap.put("text", yearStr.concat("年").concat(monthStr).concat("月"));
					monthList.add(monthMap);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return monthList;
	}
}
